package com.hll.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hll.api.exception.MyException;
import com.hll.api.service.JuheApi;
import com.hll.api.util.DateUtils;
import com.hll.api.util.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

/**
 * @author 何龙
 * @create 2019-06-02 21:51
 */
@Service
public class JuheApiImpl implements JuheApi {

	@Value("${api.juhe.baseUrl}")
	private String baseUrl;

	@Override
	public JSONArray joke(Integer page, Integer pagesize) {
		Date date = DateUtils.getStartTimeOfOneday(-300);
		HashMap<String, Object> map = new HashMap<>(4);
		map.put("sort", "asc");
		map.put("time", (date.getTime()/1000)+"");
		map.put("page", page);
		map.put("pagesize", pagesize);
		map.put("key","a36cd8760622eca429d52c08a594acb3");
		try {
			String s = HttpUtils.get(baseUrl + "/joke/content/list.php", map, 0, 0);
			if (StringUtils.isEmpty(s)) {
				throw new MyException("网络超时");
			}
			JSONObject jsonObject = JSON.parseObject(s);
			if ("SUCCESS".equals(jsonObject.getString("reason"))){
				return jsonObject.getJSONObject("result").getJSONArray("data");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new MyException("接口访问失败");
		}
		throw new MyException("接口访问失败");
	}

	@Override
	public JSONArray historyToday(String date) throws Exception {
		HashMap<String, Object> map = new HashMap<>(4);
		map.put("date", date);
		map.put("key","13cf62fab9ef3031d0421000350e7eeb");
		String s = HttpUtils.get("http://v.juhe.cn/todayOnhistory/queryEvent.php", map, 0, 0);
		if (StringUtils.isEmpty(s)){
			throw new MyException("网络超时");
		}
		JSONObject jsonObject = JSON.parseObject(s);
		if ("success".equals(jsonObject.getString("reason"))){
			return jsonObject.getJSONArray("result");
		}else {
			throw new MyException(jsonObject.getString("reason"));
		}
	}

	@Override
	public JSONArray historyTodayIfo(Integer eId) throws Exception {
		HashMap<String, Object> map = new HashMap<>(4);
		map.put("e_id", eId);
		map.put("key","13cf62fab9ef3031d0421000350e7eeb");
		String s = HttpUtils.get("http://v.juhe.cn/todayOnhistory/queryDetail.php", map, 0, 0);
		if (StringUtils.isEmpty(s)){
			throw new MyException("网络超时");
		}
		JSONObject jsonObject = JSON.parseObject(s);
		if ("success".equals(jsonObject.getString("reason"))){
			return jsonObject.getJSONArray("result");
		}else {
			throw new MyException(jsonObject.getString("reason"));
		}
	}

	@Override
	public JSONObject getIpAddress(String ip) throws Exception {
		HashMap<String, Object> map = new HashMap<>(4);
		map.put("ip", ip);
		map.put("key","c4e2456d54e1bfba9fb9cc0c134a5f30");
		String s = HttpUtils.get("http://apis.juhe.cn/ip/ipNew", map, 0, 0);
		if (StringUtils.isEmpty(s)){
			throw new MyException("网络超时");
		}
		JSONObject jsonObject = JSON.parseObject(s);
		if ("200".equals(jsonObject.getString("resultcode"))){
			return jsonObject.getJSONObject("result");
		}else {
			throw new MyException(jsonObject.getString("reason"));
		}
	}
}
